package com.shycloud.mido.member.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.SmCoursewareFolder;
import com.shycloud.mido.member.api.entity.SmCoursewareFolderCourseware;
import com.shycloud.mido.member.api.entity.SmCoursewareGroupFolder;
import com.shycloud.mido.member.api.vo.SmCoursewareFolderVO;
import com.shycloud.mido.member.mapper.SmCoursewareFolderCoursewareMapper;
import com.shycloud.mido.member.mapper.SmCoursewareFolderMapper;
import com.shycloud.mido.member.mapper.SmCoursewareGroupFolderMapper;
import com.shycloud.mido.member.mapper.SmCoursewareMapper;
import com.shycloud.mido.member.service.SmCoursewareFolderService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmCoursewareFolderServiceImpl extends ServiceImpl<SmCoursewareFolderMapper, SmCoursewareFolder> implements SmCoursewareFolderService {

	/** 课件文件夹模块 mapper */
	private SmCoursewareFolderMapper folderMapper;
	/** 课件分组和文件夹关系 mapper */
	private SmCoursewareGroupFolderMapper groupFolderMapper;
	/** 课件文件夹和课件文件关系 mapper */
	private SmCoursewareFolderCoursewareMapper folderCoursewareMapper;
	/** 课件文件模块 mapper */
	private SmCoursewareMapper coursewareMapper;

	/**
	 * 新增文件夹
	 *
	 * @param smCoursewareFolderVO
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/10/12 16:45
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public SmCoursewareFolderVO addFolder(SmCoursewareFolderVO smCoursewareFolderVO) {

		String memberId = SecurityUtils.getMember().getId();

		//判断文件夹名称是否重复
		List<SmCoursewareFolder> smCoursewareFolders = folderMapper.selectList(new QueryWrapper<SmCoursewareFolder>()
				.eq(SmCoursewareFolder.COL_NAME, smCoursewareFolderVO.getName())
				.eq("member_id", memberId));

		if (smCoursewareFolders.size() > 0) {
			throw new BusinessException("文件夹名称已存在，请重新输入");
		}

		//新增分组
		smCoursewareFolderVO.setMemberId(memberId);
		int insert = folderMapper.insert(smCoursewareFolderVO);

		if (insert > 0) {
			//获取分组ID
			Integer groupId = smCoursewareFolderVO.getGroupId();
			//添加分组-文件夹关系表
			SmCoursewareGroupFolder groupFolder = new SmCoursewareGroupFolder();
			groupFolder.setGroupId(groupId);
			groupFolder.setFolderId(smCoursewareFolderVO.getId());
			groupFolderMapper.insert(groupFolder);
		}
		return smCoursewareFolderVO;
	}


	/**
	 * 删除课件文件夹
	 *
	 * @param id
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/10/12 19:11
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean delFolder(Integer id) {

		//删除文件夹
		folderMapper.deleteById(id);
		//查询文件夹和课件文件关系信息
		List<SmCoursewareFolderCourseware> smCoursewareFolderCoursewares = folderCoursewareMapper.selectList(new QueryWrapper<SmCoursewareFolderCourseware>().eq(SmCoursewareFolderCourseware.COL_FOLDER_ID, id));
		//循环删除课件文件
		for (SmCoursewareFolderCourseware smCoursewareFolderCourseware : smCoursewareFolderCoursewares) {
			coursewareMapper.deleteById(smCoursewareFolderCourseware.getCoursewareId());
		}
		//删除文件夹和课件文件关系信息
		folderCoursewareMapper.delete(new QueryWrapper<SmCoursewareFolderCourseware>().eq(SmCoursewareFolderCourseware.COL_FOLDER_ID, id));
		//删除文件夹和文件夹分组关系表
		groupFolderMapper.delete(new QueryWrapper<SmCoursewareGroupFolder>().eq(SmCoursewareGroupFolder.COL_FOLDER_ID, id));
		return true;
	}

}

